﻿
using Xacml.Core.Interfaces;
using System.Collections;

namespace Xacml.Core.Runtime
{
  public class PolicyCombiningAlgorithmFirstApplicable : IPolicyCombiningAlgorithm
  {
    public Decision Evaluate(EvaluationContext context, IMatchEvaluableCollection policies)
    {
      foreach (IMatchEvaluable matchEvaluable in (CollectionBase) policies)
      {
        Decision decision = matchEvaluable.Evaluate(context);
        context.TraceContextValues();
        if (decision == Decision.Deny)
        {
          context.ProcessingError = false;
          context.IsMissingAttribute = false;
          return Decision.Deny;
        }
        if (decision == Decision.Permit)
        {
          context.ProcessingError = false;
          context.IsMissingAttribute = false;
          return Decision.Permit;
        }
        if (decision != Decision.NotApplicable && decision == Decision.Indeterminate)
          return Decision.Indeterminate;
      }
      return Decision.NotApplicable;
    }
  }
}
